﻿/****************************************************************************
*                                                                           *
* Copyright (c) Ionescu M. Marius. All rights reserved(2010).               *
*                                                                           *
****************************************************************************/

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace NS_Controls
{
	/// <summary>
	/// Summary description for ExtendedRepeater
	/// </summary>
	public class ExtendedRepeater : Repeater
	{
		public string SourceGroup
		{
			get
			{
				return GetGridSourceGroup(this.ClientID);
			}
			set
			{
				this.Context.Session[this.ClientID + "_SourceGroup"] = value;
			}
		}

		public string GetGridSourceGroup(string strGridName)
		{
			string strSourceGroup = strGridName;
			if (this.Context.Session[strGridName + "_SourceGroup"] != null)
				strSourceGroup = (string)this.Context.Session[strGridName + "_SourceGroup"];
			return strSourceGroup;
		}

		public DataSet GetGridDataSet(string strGridName)
		{
			string strSourceGroup = GetGridSourceGroup(strGridName);
			if (strSourceGroup == null || strSourceGroup == "")
				return null;
			else
				return ((DataSet)this.Context.Session[strSourceGroup]);
		}

		public DataView GetGridDataView(string strGridName)
		{
			object oTableIndex = this.Context.Session[strGridName + "_TableIndex"];
			DataSet ds = GetGridDataSet(strGridName);
			if (oTableIndex == null || ds == null)
				return null;
			else
			{
				DataView dv = ds.Tables[(int)oTableIndex].DefaultView;
				dv.RowFilter = this.Context.Session[strGridName + "_RowFilter"].ToString();
				dv.Sort = this.Context.Session[strGridName + "_Sort"].ToString();
				return dv;
			}
		}

		public bool SaveDataSourceOnSession
		{
			get { return (bool)(this.ViewState["SaveDataSourceOnSession"] ?? false); }
			set { this.ViewState["SaveDataSourceOnSession"] = value; }
		}

		public override object DataSource
		{
			get
			{
				if (this.SaveDataSourceOnSession)
				{
//					return this.Context.Session[this.ClientID + "_DataSource"];
					return GetGridDataView(this.ClientID);
				} 
				else
				{
					return base.DataSource;
				}
			}
			set
			{
				if (this.SaveDataSourceOnSession)
				{
					if (!(value is DataView))
						throw new Exception("Sursa repeaterului trebuie sa fie un DataView.");

//					this.Context.Session[this.ClientID + "_DataSource"] = value;

					this.Context.Session[GetGridSourceGroup(this.ClientID)] = ((DataView)value).Table.DataSet;
					this.Context.Session[this.ClientID + "_TableIndex"] = ((DataView)value).Table.DataSet.Tables.IndexOf(((DataView)value).Table);
					this.Context.Session[this.ClientID + "_RowFilter"] = ((DataView)value).RowFilter;
					this.Context.Session[this.ClientID + "_Sort"] = ((DataView)value).Sort;
				}
				else
				{
					base.DataSource = value;
				}
			}
		}
	}
}
